package com.qc31.monitorlib.ui.carreport

import autodispose2.autoDispose
import com.qc31.baselib.auto.AutoDisposeViewModel
import com.qc31.comlib.TimeUtil
import com.qc31.comlib.alarm.ReportSetModel
import com.qc31.comlib.entity.HistoryAlarm
import com.qc31.comlib.net.ServiceHelper
import com.qc31.monitorlib.R
import io.reactivex.rxjava3.subjects.PublishSubject
import java.util.*

/** @Author Jasper @Time 2020/10/15 @Desc 车辆模式报表 */
class ReportAlarmViewModel : AutoDisposeViewModel() {
    var carId = ""
    var carName = ""

    //时间类型
    var timeType = 4

    //请求数据种类
    var isAdas = false

    //开始时间
    var startTime = ""

    //结束时间
    var endTime = ""

    //是否隐鲹
    var isHidden = false

    //已选
    var choose: String = ""

    //报警响应监听
    val alarmSub by lazy { PublishSubject.create<HistoryAlarm>() }


    fun getAlarmIds(isAdas: Boolean = false): String {
        return if (isAdas) {
            getHtml(
                choose,
                ReportSetModel.instance.getAdasLength(),
                ReportSetModel.instance.getAdasSize()
            )
        } else {
            getHtml(
                choose,
                ReportSetModel.instance.getNormalLength(),
                ReportSetModel.instance.getNormalSize()
            )
        }
    }

    private fun getHtml(desc: String, length: Int, size: Int) =
        "<font color='#FF333333' >$desc </font><font color='#3791E9'> $length</font><font color='#FF333333'>/$size</font>"


    /** 获取时间 */
    private fun getTime(day: Int = 0): String {
        return if (day == 0) {
            TimeUtil.instance.getAllTime()
        } else {
            TimeUtil.instance.timeToString(Date().time - day * 24 * 3600 * 1000)
        }
    }

    /** 处理时间 */
    fun handleTime() {
        when (timeType) {
            1 -> {
                startTime = TimeUtil.instance.getAllTime(TimeUtil.getBeginDayOfYesterday().time)
                endTime = TimeUtil.instance.getAllTime(TimeUtil.getEndDayOfYesterday().time)

            }
            2 -> {
                startTime = TimeUtil.instance.getAllTime(TimeUtil.getDayBegin().time)
                endTime = TimeUtil.instance.getAllTime(TimeUtil.getDayEnd().time)
            }
            3 -> {
                startTime = getTime(2)
                endTime = getTime()
            }
            4 -> {
                startTime = getTime(6)
                endTime = getTime()
            }
        }
        getData()
    }

    /** 获取数据 */
    private fun getData() {
        if (carId.isEmpty()) {
            toast(R.string.hint_toast_choose_car)
            return
        }


        getAlarm()
    }


    /** 查询报警数据 */

    private fun getAlarm() {
        val ids = if (isAdas) ReportSetModel.instance.getAdasAlarmIds()
        else ReportSetModel.instance.getNormalAlarmIds()
        if (ids.isEmpty()) {
            toast(R.string.hint_choose_alarm_type)
            alarmSub.onNext(HistoryAlarm())
            return
        }
        toastLoading(true)
        ServiceHelper.getRepository().getHistoryAlarm(
            carId,
            startTime,
            endTime,
            ids,
            ReportSetModel.instance.getSpeed(isAdas),
            ReportSetModel.instance.getContinueTimeFormat(isAdas)
        ).autoDispose(this).subscribe({
            toastLoading(false)
            alarmSub.onNext(it)
        }, error)
    }


}
